<?php

//power by wshoto.com
if (!defined('IN_IA')) {
    die( 'Access Denied' );
}

/*
# PLUGIN BASE INSERT SQL
INSERT INTO `ims_ewei_shop_plugin` (`id`, `displayorder`, `identity`, `name`, `version`, `author`, `status`) VALUES
(11, 11,  'express',	'快递预处理',	'1.0',	'官方', '1');

# PLUGIN DATA INSERT SQL

# 物流基本信息
CREATE TABLE ims_ewei_shop_plugin_express
(
    id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
    uniacid INT NOT NULL COMMENT '公众号识别ID',
    dispatchid INT NOT NULL COMMENT '配送快递ID',
    sender VARCHAR(20) NOT NULL COMMENT '发件人名称',
    phone VARCHAR(15) NOT NULL COMMENT '发件人号码',
    province VARCHAR(15) NOT NULL COMMENT '发货者省份',
    city VARCHAR(15) NOT NULL COMMENT '发货者城市',
    district VARCHAR(15) NOT NULL COMMENT '发货者区域',
    address VARCHAR(255) NOT NULL COMMENT '发件人详细地址'
);

CREATE INDEX uniacid_index ON ims_ewei_shop_plugin_express (uniacid);
CREATE INDEX dispatchid_index ON ims_ewei_shop_plugin_express (dispatchid);

# 物流单号池列表
CREATE TABLE ims_ewei_shop_plugin_express_courier
(
    id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
    uniacid INT(11) NOT NULL COMMENT '公众号识别ID',
    expressid INT(11) NOT NULL COMMENT '所属快递基础信息ID',
    identification VARCHAR(10) COMMENT '物流单号字母位',
    start_number VARCHAR(100) COMMENT '订单起始编号',
    end_number VARCHAR(100) COMMENT '订单结束编号'
);
CREATE INDEX uniacid_index ON ims_ewei_shop_plugin_express_courier (uniacid);
CREATE INDEX expressid_index ON ims_ewei_shop_plugin_express_courier (expressid);

# 物流单号池明细
CREATE TABLE ims_ewei_shop_plugin_express_courier_lists
(
    id INT(11) PRIMARY KEY AUTO_INCREMENT,
    uniacid INT(11) NOT NULL COMMENT '公众号识别ID',
    courierid INT(11) NOT NULL COMMENT '所属单号池ID',
    number INT(11) NOT NULL COMMENT '对应单号',
    orderid INT(11) DEFAULT 0  COMMENT '对应订单ID',
    goodsid INT(11) DEFAULT 0  COMMENT '对应订单商品ID',
    status TINYINT(3) DEFAULT 0 NOT NULL COMMENT '使用状态， 0: 未使用 1:已使用'
);
CREATE INDEX uniacid_index ON ims_ewei_shop_plugin_express_courier_lists (uniacid);
CREATE INDEX courierid_index ON ims_ewei_shop_plugin_express_courier_lists (courierid);
CREATE INDEX orderid_index ON ims_ewei_shop_plugin_express_courier_lists (orderid);
CREATE INDEX goodsid_index ON ims_ewei_shop_plugin_express_courier_lists (goodsid);
CREATE INDEX status_index ON ims_ewei_shop_plugin_express_courier_lists (status);

*/

require_once EWEI_SHOP_PLUGIN .'express/inc/functions.php';
require_once EWEI_SHOP_PLUGIN . 'express/model.php';

/**
 * Class ExpressWeb
 */
class ExpressWeb extends Plugin {

    public function __construct(){
        parent::__construct('express');
        $this->model = new ExpressModel();  // 此处覆盖父类model，仅为了IDE自动跟踪。
    }

    /**
     * 快递预处理设置默认
     */
    public function index(){
        $this->express();
    }

    /**
     * 快递预处理设置
     */
    public function express(){
        global $_GPC;

        if($_GPC['op'] == 'delete'){
            $this->_expressDelete();
            exit();
        }

        if($_GPC['op'] == 'editor'){
            $this->_expressEditor();
            exit();
        }

        $pluginExpress = new express();
        $expressLists = $pluginExpress->getExpressLists();

        include $this->template('express');
    }

    /**
     * 物流单号列表入口
     */
    public function courier(){
        global $_W, $_GPC;

        if($_GPC['op'] == 'editor'){
            $this->_courierEditor();
            exit();
        }

        if($_GPC['op'] == 'delete'){
            $this->_courierDelete();
            exit();
        }

        if($_GPC['op'] == 'create'){
            $this->_createCourierLists();
            exit();
        }

        if($_GPC['op'] == 'show'){
            $this->_showCourierLists();
            exit();
        }

        $expressId = intval($_GPC['expressid']);

        if(empty($expressId)){
            message('错误的URL访问，请重试。', $this->createPluginWebUrl('express/express'), 'info');
        }

        $pluginCourierModel = new courier();
        $courierLists = $pluginCourierModel->getCourierLists($expressId);

        include $this->template('courier');
    }

    /**
     * 批量发货入口
     */
    public function shipment(){
        global $_W, $_GPC;

        if($_GPC['op'] == 'lists'){
            $this->_shipmentLists();
            exit();
        }

        $expressModel = new express();
        $dispatchLists = $expressModel->getDispatchLists();

        $starttime = strtotime(date('Y-m-d'));
        $endtime = strtotime(date('Y-m-d') . ' +1 day');

        $pstarttime = time();
        $pendtime = strtotime(date('Y-m-d') . ' +1 day');

        load()->func('tpl');
        include $this->template('shipment');
    }

    private function _shipmentLists(){
        global $_W, $_GPC;

        if($_W['token'] != $_GPC['token']){
            message('无法通过外部链接直接打开');
        }

        if(!empty($_GPC['createtime']) && !empty($_GPC['paytime'])){
            message('无法同时使用两个时间条件搜索');
        }

        $where = array();

        if(!empty($_GPC['dispatchid'])){
            $where['dispatchid']  = array('EQ' => intval($_GPC['dispatchid']));
        }else{
            message('请选择订单对应的快递');
        }

        if(!empty($_GPC['send']) && intval($_GPC['status']) == 2){
            message('无法填充状态为已发货的订单，请重新选择。');
        }

        if(!empty($_GPC['export']) && intval($_GPC['status']) == 1){
            message('无法导出状态为未发货的订单，请重新选择。');
        }

        if(!empty($_GPC['ordersn'])){
            $where['ordersn'] = array('LIKE' => trim($_GPC['ordersn']));
        }
        if (!empty( $_GPC['createtime'] ) || !empty( $_GPC['paytime'] )) {
            if (isset( $_GPC['time'] )) {

                $time = array( 'EGT' => strtotime($_GPC['time']['start']), 'ELT' => strtotime($_GPC['time']['end']) );
                $time_paytime = array( 'EGT' => strtotime($_GPC['ptime']['start']), 'ELT' => strtotime($_GPC['ptime']['end']) );

                if (!empty( $_GPC['createtime'] )) {
                    $where['createtime'] = $time;
                }

                if (!empty( $_GPC['paytime'] )) {
                    $where['paytime'] = $time_paytime;
                }

            }
        }

        // 1为已付款，2为已发货，3为成功
        if(isset($_GPC['status'])){
            $where['status'] = array('EQ' => intval($_GPC['status']));
        }

        $where['isverify'] = array('EQ' => 0);
        $starttime = strtotime($_GPC['time']['start']);
        $endtime = strtotime($_GPC['time']['end']);
        $pstarttime = strtotime($_GPC['ptime']['start']) ? strtotime($_GPC['ptime']['start']) : time();
        $pendtime =  strtotime($_GPC['ptime']['end']) ? strtotime($_GPC['ptime']['end']) : time();


        $shipmentModel = new shipment();
        $shipmentModel->setPage(max(1, intval($_GPC['page'])));
        $shipmentModel->setSize(10);

        if(!empty($_GPC['send'])){

            $shipmentModel->setSize(500);

            $field = array('id');
            $result = $shipmentModel->where($where)->field($field)->getOrderLists();

            $backUrl = getBackUrl();
            $backUrl = str_replace('&page='.$shipmentModel->page, '', $backUrl);

            if(empty($result['result'])){
                $backUrl = str_replace('&send=1', '', $backUrl);
                message('找不到需要发货的订单 / 发货完成了', $backUrl, 'success');
            }

            if($shipmentModel->page == 1){
                $field = array('id');
                $resultAll = $shipmentModel->where($where)->field($field)->getOrderLists(false);

                if(empty($resultAll)){
                    message('没有可以发货的订单，请更换您的查询条件。');
                }

                $this->_shipmentSendCheck($resultAll, intval($_GPC['dispatchid']));
            }

            $sendStatus = $this->_shipmentSend($result['result'], intval($_GPC['dispatchid']));

            if($sendStatus == true){
                $nextPage = $shipmentModel->page + 1;
                $backUrl .= '&page='.$nextPage;
                message('正在为您填充物流单，请耐心等待，不要关闭浏览器。', $backUrl, 'success');
            }else{
                $backUrl = str_replace('&send=1', '', $backUrl);
                message($sendStatus, $backUrl, 'error');
            }

            exit();
        }

        if(!empty($_GPC['export'])){
            $field = array('id');
            $result = $shipmentModel->where($where)->field($field)->getOrderLists(false);

            if(empty($result)){
                message('不存在需要导出的订单');
            }

            $this->_shipmentExport($result, intval($_GPC['dispatchid']));
            exit();
        }


        $field = array('id', 'ordersn', 'createtime', 'sendtime', 'paytime');
        $result = $shipmentModel->where($where)->field($field)->getOrderLists();

        $pagination = $result['pagination'];
        $orderLists = $result['result'];

        $expressModel = new express();
        $dispatchLists = $expressModel->getDispatchLists();

        load()->func('tpl');
        include $this->template('shipment');
    }

    /**
     * 批量发货前的订单数量检测
     * @param $orderLists
     * @param $dispatchid
     * @return bool
     */
    private function _shipmentSendCheck($orderLists, $dispatchid){

        $orderLists = array_keys($orderLists);

        if(empty($orderLists)){
            return false;
        }

        $shipmenetModel = new shipment();
        $goodsTotal = $shipmenetModel->getGoodsSumByOrderIds($orderLists);

        $expressModel = new express();
        $expressId = $expressModel->getExpressIdByDispatchid($dispatchid);

        $courierModel = new courier();
        $listsTotalResult = $courierModel->getCourierListsTotal($expressId);

        if($listsTotalResult['total'] < $goodsTotal){
            message('订单池数量无法满足发货需要，请填充单号池，或缩小订单查询时间范围。');
        }

        return true;
    }

    /**
     * 批量发货
     * @param $orderLists
     * @param $dispatchId
     * @return bool
     */
    private function _shipmentSend($orderLists, $dispatchId){

        @ini_set('memory_limit', '1024M');
        global $_W, $_GPC;

        $orderIds = array_keys($orderLists);

        $shipmenetModel = new shipment();
        $goodsTotal = $shipmenetModel->getGoodsSumByOrderIds($orderIds);

        $expressModel = new express();
        $expressId = $expressModel->getExpressIdByDispatchid($dispatchId);
        $dispatchDetail = $expressModel->getDispatch($dispatchId);

        $courierModel = new courier();
        $courierLists = $courierModel->getCourierLists($expressId);

        $courierIds = array();
        $listsTotal = 0;

        if(!empty($courierLists)){
            foreach($courierLists as $v){
                if(!empty($v['oth']) && $goodsTotal >= $listsTotal){
                    $courierIds[] = $v['id'];
                    $listsTotal += $v['oth'];
                }

                if($goodsTotal <= $listsTotal){
                    break;
                }
            }
        }
        // 所需物流单号池
        $courierListsx = $courierModel->getCourierListsByTotal($courierIds, $goodsTotal);

        $orderGoods = $shipmenetModel->getGoodsByOrderIds($orderIds);

        $ogids = array();
        $current = 0;
        $time = time();

        if(!empty($orderGoods)){

            try{
                pdo_begin();

                foreach($orderGoods as $og){
                    if (in_array($og['id'], $ogids)) {
                        continue;
                    }

                    if (!empty( $og['total'] ) && !empty( $og['orderid'] )) {
                        for ($i = 0; $i < $og['total']; $i++) {

                            $courier = $courierListsx[$current];

                            $courierStatus = pdo_update('ewei_shop_plugin_express_courier_lists', array( 'orderid' => $og['orderid'], 'goodsid' => $og['goodsid'], 'status' => 1 ), array( 'id' => $courier['id'] ));

                            if($courierStatus == false){
                                throw new PDOException('更新物流单号状态失败。');
                            }

                            if ($i == 0) {
                                $orderStatus = pdo_update('ewei_shop_order', array( 'expresscom' => $dispatchDetail['dispatchname'], 'expresssn' => $courier['number'], 'express' => $dispatchDetail['express'], 'sendtime' => $time, 'status' => 2 ), array( 'id' => $og['orderid'], 'uniacid' => $_W['uniacid'] ));

                                if($orderStatus !== false){
                                    $refund = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_order_refund') . ' where id=:id limit 1', array(':id' => $og['refundid']));

                                    if (!empty($refund)) {
                                        pdo_update('ewei_shop_order_refund', array('status' => -1), array('id' => $og['refundid']));
                                        pdo_update('ewei_shop_order', array('refundid' => 0), array('id' => $og['orderid']));
                                    }

                                    //m('notice')->sendOrderMessage($og['orderid']);
                                    plog('order.op.send', "订单自动发货 ID: {$og['orderid']} 订单号: {$og['ordersn']} <br/>快递公司: {$dispatchDetail['dispatchname']} 快递单号: {$courier['number']}");
                                }else{
                                    throw new PDOException('更新订单状态失败。');
                                }
                            }

                            $current++;
                        }
                    }

                    $ogids[] = $og['id'];
                }

                pdo_commit();
            }catch(PDOException $e){
                pdo_rollback();
                return $e->getMessage();
            }
        }

        return true;
    }

    /**
     * 批量发货导出
     * @param $orderIds
     * @param $dispatchId
     */
    private function _shipmentExport($orderIds, $dispatchId){
        global $_W, $_GPC;

        $orderIds = array_keys($orderIds);

        $expressModel = new express();
        $dispatchDetail = $expressModel->getDispatch($dispatchId);
        $expressDetail = $expressModel->getExpressDetailByDispatchid($dispatchId);
        $columns = $expressModel->getTemplate($dispatchDetail['express']);

        $shipmentModel = new shipment();
        $result = $shipmentModel->getSendInfoByOrderIds($orderIds);

        $courierModel = new courier();

        $lists = array();
        foreach($result as $key => $val){
            $temp = array();

            $courierLists = $courierModel->getListsByOrderId($val['id'], $val['goodsid']);

            $temp['sh'] = $val['id'];
            $temp['sender'] = $expressDetail['sender'];
            $temp['phone'] = $expressDetail['phone'];
            $temp['senderprovince'] = $expressDetail['province'];
            $temp['sendercity'] = $expressDetail['city'];
            $temp['senderdistrict'] = $expressDetail['district'];
            $temp['senderaddress'] = $expressDetail['address'];
            $temp['goods_title'] = $val['title'];
            $temp['realname'] = $val['realname'];
            $temp['mobile'] = $val['mobile'];
            $temp['province'] = $val['province'];
            $temp['city'] = $val['city'];
            $temp['area'] = $val['area'];
            $temp['address'] = $val['province'].$val['city'].$val['area'].$val['address'];
            $temp['total'] = '1';
            $temp['goodsprice'] = $val['goodsprice'];//总价
            $temp['goodspicprice'] = $val['goodsprice']/$val['total'];//单价
            $temp['goodsweight'] = $val['weight'];//重量
            $temp['goodstype'] = '纸';//类型
            $temp['remark'] = $val['remark'];

            if($val['total'] > 1){

                for($i = 0; $i < $val['total']; $i++){

                    if(!isset($courierLists[$i])){
                        continue;
                    }

                    $temp['ordersn'] = $val['ordersn'].'-'.$val['goodsid'].'-'.$val['total'].'-'.($i+1);
                    $temp['expresssn'] = $courierLists[$i]['number'];
                    $lists[] = $temp;
                }

            }else{
                $temp['expresssn'] = $courierLists[0]['number'];
                $temp['ordersn'] = $val['ordersn'].'-'.$val['goodsid'];
                $lists[] = $temp;
            }
        }
        $excelModel = new excel();
        $excelModel->export($lists, array("title" => "批量发货-" . date('Y-m-d-H-i', time()), "columns" => $columns));
    }

    /**
     * 快递预处理设置编辑
     */
    private function _expressEditor(){
        global $_W, $_GPC;

        if(checksubmit()){
            $this->_expressPost();
        }

        $pluginExpress = new express();
        $dispatchLists = $pluginExpress->getDispatchLists();

        if(empty($dispatchLists)){
            message('请先添加配送方式', $this->createWebUrl('shop', array('op' => 'post', 'p' => 'dispatch')), 'info');
        }

        if(!empty($_GPC['id'])){
            $expressId = intval($_GPC['id']);

            if(!empty($expressId)){
                $express = $pluginExpress->getExpressDetail($expressId);
                empty($express) && message('未找到可以编辑的信息，请重试。', $this->createPluginWebUrl('express/express'), 'info');
            }else{
                message('错误的URL访问，请重试。', $this->createPluginWebUrl('express/express'), 'info');
            }
        }

        load()->func('tpl');
        include $this->template('expressEditor');
    }

    /**
     * 快递预处理设置删除处理
     */
    private function _expressDelete(){
        global $_W, $_GPC;

        $expressId = intval($_GPC['id']);

        if(empty($expressId)){
            message('错误的URL访问，请重试。', $this->createPluginWebUrl('express/express'), 'info');
        }

        $status = pdo_delete('ewei_shop_plugin_express', array('id' => $expressId, 'uniacid' => $_W['uniacid']));

        if($status == true){
            message('恭喜您，删除快递预处理基础配置成功', $this->createPluginWebUrl('express/express'), 'success');
        }

        message('出现了一个未知的错误，可能是您正在删除一个不存在的数据。', $this->createPluginWebUrl('express/express'));
    }

    /**
     * 快递预处理设置提交处理
     */
    private function _expressPost(){
        global $_W;

        $express = $this->_expressPostFilter();

        if(empty($express['id'])){
            $status = pdo_insert('ewei_shop_plugin_express', $express);

            if($status == true){
                message('恭喜您，添加了一个快递预处理基础配置', $this->createPluginWebUrl('express/express'), 'success');
            }
        }else{
            $expressId = $express['id'];
            unset($express['id']);
            unset($express['dispatchid']);

            $status = pdo_update('ewei_shop_plugin_express', $express, array('id' => $expressId, 'uniacid' => $_W['uniacid']));

            if($status == true){
                message('恭喜您，更新快递预处理基础配置成功', $this->createPluginWebUrl('express/express'), 'success');
            }
        }

        message('出现了一个未知的错误，请重新尝试');
    }

    /**
     * 快递预处理设置过滤处理
     */
    private function _expressPostFilter(){
        global $_W, $_GPC;

        $express = $_GPC['express'];

        if(empty($express['id'])){
            empty($express['dispatchid']) && message('请选择正确的配送方式');
        }

        if(!empty($express['dispatchid'])){
            $expressModel = new express();
            $expressId = $expressModel->getExpressIdByDispatchid(intval($express['dispatchid']));

            if(!empty($expressId)){
                message('该物流已经添加过了，请不要重复添加。');
            }
        }

        empty($express['sender']) && message('请输入正确的发件人姓名');
        empty($express['phone']) && message('请选择正确的手机号码');
        empty($express['area']['province']) && message('请选择正确的发货省份');
        empty($express['area']['city']) && message('请选择正确的发货城市');
        empty($express['area']['district']) && message('请选择正确的发货区域');
        empty($express['address']) && message('请输入正确的发货详细地址');

        $express['province'] = $express['area']['province'];
        $express['city'] = $express['area']['city'];
        $express['district'] = $express['area']['district'];
        $express['uniacid'] = $_W['uniacid'];

        unset($express['area']);

        return $express;
    }

    /**
     * 单号池编辑页面
     * @param $expressId
     */
    private function _courierEditor(){
        global $_W, $_GPC;

        $expressId = intval($_GPC['expressid']);

        if(empty($expressId)){
            message('错误的URL访问，请重试。', $this->createPluginWebUrl('express/express'), 'info');
        }

        if(checksubmit()){
            $this->_courierPost();
        }

        $numberId = intval($_GPC['id']);

        if(!empty($numberId)){
            $pluginCourierModel = new courier();
            $courier = $pluginCourierModel->getCourierDetail($expressId, $numberId);

            if(empty($courier)){
                message('错误，您正在编辑一个不存在的数据。');
            }
        }

        include $this->template('courierEditor');
    }

    /**
     * 单号池数据提交处理
     * @param $expressId
     */
    private function _courierPost(){

        $courier = $this->_courierPostFilter();


        if(empty($courier['id'])){
            $status = pdo_insert('ewei_shop_plugin_express_courier', $courier);

            if($status == true){

                $data = array(
                    'op' => 'create',
                    'courierid' => pdo_insertid(),
                    'expressid' => $courier['expressid'],
                    'start_number' => $courier['start_number'],
                    'end_number' => $courier['end_number'],
                );

                $backUrl = $this->createPluginWebUrl('express/courier', $data);
                message('恭喜您，添加单号段成功，请不要关闭', $backUrl, 'success');
            }
        }
        /* TODO : 是否给予编辑功能？
        else{
            $courierId = $courier['id'];
            unset($courier['id']);

            $status = pdo_update('ewei_shop_plugin_express_courier', $courier, array('id' => $courierId));

            if($status == true){

                $data = array(
                    'op' => 'create',
                    'courierid' => pdo_insertid(),
                    'count' => intval($courier['start_number'] - $courier['end_number'])
                );

                $backUrl = $this->createPluginWebUrl('express/courier', $data);

                message('恭喜您，更新了一个单号池。', $backUrl, 'success');
            }
        }*/

        message('未知错误，请重新尝试。', '', 'error');
    }

    /**
     * 单号池数据提交过滤
     * @return array
     */
    private function _courierPostFilter(){
        global $_W, $_GPC;

        $courier = $_GPC['courier'];

        empty($courier['expressid']) && message('无法定位所属快递', '', 'error');
        empty($courier['start_number']) && message('请输入起始单号', '', 'error');
        empty($courier['end_number']) && message('请输入结束单号', '', 'error');

        if(intval($courier['start_number']) >= intval($courier['end_number'])){
            message('结束单号不能小于或等于起始单号', '', 'error');
        }

        $courier['uniacid'] = $_W['uniacid'];

        return $courier;
    }

    /**
     * 删除单号池列表
     */
    private function _courierDelete(){
        global $_W, $_GPC;

        $expressId = intval($_GPC['expressid']);
        $courierId = intval($_GPC['courierid']);
        $id = intval($_GPC['id']);

        if(empty($expressId) || empty($courierId) || empty($id)){
            message('错误的URL访问，请重试。', '', 'info');
        }

        $status = pdo_delete('ewei_shop_plugin_express_courier_lists', array('id' => $id, 'courierid' => $courierId));

        if($status == true){
            $backUrl = $this->createPluginWebUrl('express/courier', array('op' => 'show', 'expressid' => $expressId, 'id' => $courierId, ));
            message('恭喜您，删除单号成功。', $backUrl, 'success');
        }

        message('出现了一个未知的错误，可能是您正在删除一个不存在的数据。');

    }

    /**
     * 创建单号池详细编号
     */
    private function _createCourierLists(){
        global $_W, $_GPC;

        $startNumber = intval($_GPC['start_number']);
        $endNumber = intval($_GPC['end_number']);

        $page = !empty($_GPC['page']) ? intval($_GPC['page']) : 1;
        $total = $endNumber - $startNumber;

        $size = 10000;

        $start = ($page - 1) * $size;

        $tempNumber = $startNumber + $start;

        try{
            pdo_begin();

            for($i = 0; $i < $size; $i++){

                if(($tempNumber + $i) >= $endNumber){
                    break;
                }

                $data = array();
                $data = array('courierid' => $_GPC['courierid'], 'number' => $tempNumber + $i, 'uniacid' => $_W['uniacid']);

                pdo_insert('ewei_shop_plugin_express_courier_lists', $data);
            }

            pdo_commit();

        }catch(PDOException $e){
            pdo_rollback();
            echo $e->getMessage();
        }

        $urlParams = array(
            'op' => 'create',
            'courierid' => $_GPC['courierid'],
            'expressid' => $_GPC['expressid'],
            'start_number' => $startNumber,
            'end_number' => $endNumber,
            'page' => $page + 1,
        );

        $surplus = $total - $page * $size;

        if($surplus <= 0){
            $backUrl = $this->createPluginWebUrl('express/courier', array('op' => 'lists', 'expressid' => $_GPC['expressid']));
            message('恭喜您，已经更新完成', $backUrl, 'success');
        }

        $doCount = $total - $surplus;

        $backUrl = $this->createPluginWebUrl('express/courier', $urlParams);
        message('正在为您填充单号池数据，已完成：'.$doCount.' 条。', $backUrl, 'success');

    }

    /**
     * 查看单号池列表
     */
    private function _showCourierLists(){
        global $_W, $_GPC;

        $pindex = max(1, intval($_GPC['page']));
        $id = intval($_GPC['id']);
        $whereSql = '';

        if(!empty($_GPC['number'])){
            $whereSql .= ' AND `number` like ("%'.$_GPC['number'].'%") ';
        }

        if(isset($_GPC['status'])){
            $whereSql .= ' AND `status` = '.intval($_GPC['status']);
        }

        $pluginCourierModel = new courier();
        $courierLists = $pluginCourierModel->getCourierDetail($id, $pindex, 20, $whereSql);

        $pagination = $courierLists['pagination'];
        $detailLists = $courierLists['detail'];

        unset($courierLists);

        include $this->template('courierDetail');
    }

}